package com.stargazer.ming.ui;

import com.stargazer.ming.R;
import com.stargazer.ming.ui.util.CameraUtil;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainUI extends Activity {
	public static final String TAG = "Ming.MainUI";
	
	public static final int CONTEXT_MENU_IMAGE = Menu.FIRST + 1;
	public static final int CONTEXT_MENU_TAKE_PICTURE = CONTEXT_MENU_IMAGE + 1;
	public static final int CONTEXT_MENU_CROP_PICTURE = CONTEXT_MENU_TAKE_PICTURE + 1;
	
	private ListView mContentLV;
	
	private ContentAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        setContentView(R.layout.activity_main_ui);
        mContentLV=(ListView) findViewById(R.id.content_lv);
        
        mAdapter=new ContentAdapter(this,getResources().getStringArray(R.array.content_list));
        mContentLV.setAdapter(mAdapter);
        
        mContentLV.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				//market
				case 0:
					startActivity(new Intent(MainUI.this, MarketUI.class));
					break;
				//make postcard
				case 1:
					CameraUtil.showPhotoDialog(MainUI.this);
					break;

				default:
					break;
				}
				
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main_ui, menu);
        return true;
    }
    
    
    
    @Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	final Intent intent=new Intent(this, CropImageUI.class);
		switch (requestCode) {
		case MainUI.CONTEXT_MENU_IMAGE:
			intent.putExtra(ConstantsUI.IMAGE_PATH,CameraUtil.resolvePhotoFromIntent(getApplicationContext(), data));
			break;
		case MainUI.CONTEXT_MENU_TAKE_PICTURE:
			intent.putExtra(ConstantsUI.IMAGE_PATH, CameraUtil.getResultPhotoPath(getApplicationContext(),data));
			break;
		default:
			break;
		}
		startActivity(intent);
	}
	
	private class ContentAdapter extends BaseAdapter{
		public class ViewHolder {
			public TextView contentNameTV;
		}
		
		private Context mContext;
		private String[] mContentList=null;
		
		public ContentAdapter(Context context,String[] contentList){
			mContext=context;
			mContentList=contentList;
		}

		@Override
		public int getCount() {
			return mContentList.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, final ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(mContext,R.layout.content_item,null);
				ViewHolder viewHolder = new ViewHolder();
				viewHolder.contentNameTV = (TextView) convertView.findViewById(R.id.content_name_tv);
				convertView.setTag(viewHolder);
			}

			ViewHolder holder = (ViewHolder) convertView.getTag();
			holder.contentNameTV.setText(mContentList[position]);

			return convertView;
		}
	}
}
